home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / csrc1.arc / COORNO.C < prev    next >
C/C++ Source or Header  |  1989-07-27  |  2KB  |  135 lines

  1. /*
  2.  * coorno
  3.  */
  4.  
  5. /*)BUILD    $(TKBOPTIONS) = {
  6.             TASK    = ...CTX
  7.         }
  8. */
  9.  
  10. #ifdef    DOCUMENTATION
  11.  
  12. title    coorno    Cookie text file formatter
  13. index        Cookie text file formatter
  14.  
  15. synopsis
  16.  
  17.     coorno [name ...]
  18.  
  19. description
  20.  
  21.     Concatenate each file, making sure that the last
  22.     cookie of each file is terminated by '%%' and
  23.     that there are no null-cookies.
  24.     
  25.     cootxt accepts wild-card file name arguments; if no
  26.     files are given, it copies stdin.
  27.  
  28.     The output is in runoff format.
  29.  
  30. diagnostics
  31.  
  32.     .lm +8
  33.     .s.i -8;"file name": cannot open
  34.     .s.i -8;"file name": illegal file name
  35.     .lm -8
  36.  
  37. author
  38.  
  39.     Martin Minow
  40.  
  41. bugs
  42.  
  43. #endif
  44.  
  45. #include <stdio.h>
  46.  
  47. char    line[513];
  48. int    ncookies = 0;
  49.  
  50. main(argc, argv)
  51. int        argc;
  52. char        *argv[];
  53. {
  54.     register int    i, nfiles;
  55.     register FILE    *fp;
  56.     int        gotcha;
  57.  
  58.     nfiles = 0;
  59.     puts(".lm 0.rm 72.nf");
  60.     if (argc < 2) {
  61.         ++nfiles;
  62.         process(stdin);
  63.     }
  64.     else {
  65. #ifdef unix
  66.         for (i = 1; i < argc; ++i) {
  67.         if ((fp = fopen(argv[i], "r")) == NULL) {
  68.             perror(argv[i]);
  69.         }
  70.         else {
  71.             ++nfiles;
  72.             process(fp);
  73.             fclose(fp);
  74.         }
  75.         }
  76. #else
  77.         for (i = 1; i < argc; ++i) {
  78.         if ((fp = fwild(argv[i], "r")) == NULL) {
  79.             perror(argv[i]);
  80.         }
  81.         else {
  82.             for (gotcha = 0; fnext(fp) != NULL; gotcha++) {
  83.             ++nfiles;
  84.             process(fp);
  85.             }
  86.             if (gotcha == 0) {
  87.             fprintf(stderr, "\"%s\": no matching files\n",
  88.                 argv[i]);
  89.             }
  90.         }
  91.         }
  92. #endif
  93.     }
  94. }
  95.  
  96. int percent = TRUE;
  97.  
  98. process(fp)
  99. FILE        *fp;            /* File pointer            */
  100. {
  101.     register int    flag;
  102.     register char    *tp;
  103.  
  104.     while (fgets(line, sizeof line, fp) != NULL) {
  105.         if (percent && line[0] == '\n')
  106.         continue;
  107.         flag = (line[0] == '%' && line[1] == '%');
  108.         if (flag && percent)
  109.         continue;
  110.         if (flag)
  111.         puts(".tp 6.s");
  112.         else {
  113.         for (tp = line; *tp != EOS; tp++) {
  114.             switch (*tp) {
  115.             case '\\':
  116.             case '_':
  117.             case '^':
  118.             case '#':
  119.             case '%':
  120.             case '&':
  121.             putchar('_');
  122.             default:
  123.             putchar(*tp);
  124.             }
  125.         }
  126.         }
  127.         percent = flag;
  128.     }
  129.     if (!percent) {
  130.         puts(".tp 6.s");
  131.         percent = TRUE;
  132.     }
  133. }
  134.  
  135.